package cn.com.base.lpg.common.utils;

import cn.com.base.lpg.reportForm.data.vo.ReportFormDynamicTitleVo;
import cn.com.framework.core.exception.OperationException;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.write.metadata.WriteSheet;
import org.apache.commons.codec.Charsets;
import org.apache.commons.compress.utils.IOUtils;
import org.springframework.core.io.ClassPathResource;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.InputStream;
import java.net.URLEncoder;
import java.util.List;

public class ExcelExportUtil {

    public static <T> void exportList(HttpServletResponse response, String filePath, String fileName, String sheetName,
                                      List<T> dataList) {
        //获取模板(模板你可以放在任何位置，前提是你能获取到。这里放在resource下)
        ClassPathResource couponOrderTemplateResource = new ClassPathResource(filePath);
        InputStream templateInputStream = null;
        ExcelWriter excelWriter = null;
        ServletOutputStream outputStream = null;
        try {
            response.setContentType("application/vnd.ms-excel");
            response.setCharacterEncoding(Charsets.UTF_8.name());
            fileName = URLEncoder.encode(fileName, Charsets.UTF_8.name());
            response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
            outputStream = response.getOutputStream();
            templateInputStream = couponOrderTemplateResource.getInputStream();
            excelWriter = EasyExcel.write(outputStream).withTemplate(templateInputStream).build();
            WriteSheet writeSheet = EasyExcel.writerSheet(sheetName).needHead(Boolean.FALSE).build();
            excelWriter.fill(dataList, writeSheet);
            //关闭
            excelWriter.finish();
        } catch (Exception e) {
            throw new OperationException(e.getMessage());
        }
        IOUtils.closeQuietly(templateInputStream);
        IOUtils.closeQuietly(outputStream);
        IOUtils.closeQuietly(excelWriter);
    }


    /**
     *
     * @param response
     * @param filePath
     * @param fileName
     * @param sheetName
     * @param dynamicFormTitle
     * @param dataList
     * @param <T>
     */
    public static <T> void exportListForDynamicTitle(HttpServletResponse response, String filePath, String fileName, String sheetName,
                                                     ReportFormDynamicTitleVo dynamicFormTitle, List<T> dataList) {
        //获取模板(模板你可以放在任何位置，前提是你能获取到。这里放在resource下)
        ClassPathResource couponOrderTemplateResource = new ClassPathResource(filePath);
        InputStream templateInputStream = null;
        ExcelWriter excelWriter = null;
        ServletOutputStream outputStream = null;
        try {
            response.setContentType("application/vnd.ms-excel");
            response.setCharacterEncoding(Charsets.UTF_8.name());
            fileName = URLEncoder.encode(fileName, Charsets.UTF_8.name());
            response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
            outputStream = response.getOutputStream();
            templateInputStream = couponOrderTemplateResource.getInputStream();
            excelWriter = EasyExcel.write(outputStream).withTemplate(templateInputStream).build();
            WriteSheet writeSheet = EasyExcel.writerSheet(sheetName).needHead(Boolean.FALSE).build();
            /**动态表头**/
            excelWriter.fill(dynamicFormTitle, writeSheet);
            /**真实数据**/
            excelWriter.fill(dataList, writeSheet);
            //关闭
            excelWriter.finish();
        } catch (Exception e) {
            throw new OperationException(e.getMessage());
        }
        IOUtils.closeQuietly(templateInputStream);
        IOUtils.closeQuietly(outputStream);
        IOUtils.closeQuietly(excelWriter);
    }

        /**
     *
     * @param response
     * @param filePath
     * @param fileName
     * @param sheetName
     * @param mapEntity
     * @param dataList
     * @param <T>
     */
    public static <T> void exportFormAndListView(HttpServletResponse response, String filePath, String fileName, String sheetName,
                                                     Object mapEntity, List<T> dataList) {
        //获取模板(模板你可以放在任何位置，前提是你能获取到。这里放在resource下)
        ClassPathResource couponOrderTemplateResource = new ClassPathResource(filePath);
        InputStream templateInputStream = null;
        ExcelWriter excelWriter = null;
        ServletOutputStream outputStream = null;
        try {
            response.setContentType("application/vnd.ms-excel");
            response.setCharacterEncoding(Charsets.UTF_8.name());
            fileName = URLEncoder.encode(fileName, Charsets.UTF_8.name());
            response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
            response.setHeader("Access-Control-Expose-Headers", "Content-Disposition");
            response.setHeader("Content-Type","application/octet-stream;charset=utf-8");

            outputStream = response.getOutputStream();
            templateInputStream = couponOrderTemplateResource.getInputStream();
            excelWriter = EasyExcel.write(outputStream).withTemplate(templateInputStream).build();
            WriteSheet writeSheet = EasyExcel.writerSheet(sheetName).needHead(Boolean.FALSE).build();
            /**map表单**/
            excelWriter.fill(mapEntity, writeSheet);
            /**list数据**/
            FillConfig fillConfig = FillConfig.builder().forceNewRow(Boolean.TRUE).build();
            excelWriter.fill(new FillWrapper("detailList", dataList),fillConfig,writeSheet);
            //关闭
            excelWriter.finish();
        } catch (Exception e) {
            throw new OperationException(e.getMessage());
        }
        IOUtils.closeQuietly(templateInputStream);
        IOUtils.closeQuietly(outputStream);
        IOUtils.closeQuietly(excelWriter);
    }
}
